<template>
    <view class="wrap">
        <view class="Info">
            <view class="item" @click="upImg">
                <text>头像</text>
                <image class="u-avatar" :src="avatarUrl" mode="aspectFill"></image>
                <view class="iconfont icon-dayuhao icon"></view>
            </view>
            <view class="item">
                <text>昵称</text>
                <input type="text" v-model="nickName" placeholder="未填写" maxlength="16" />
            </view>
        </view>
        <view class="randInfo" @click="getRandUser()">
            <text class="iconfont icon-suijibofang randIcon"></text>
            <text>随机头像与昵称</text>
        </view>
        <button type="primary" class="btn" :disabled="disabled" @click="updateUserInfo">保存</button>

    </view>
</template>

<script>
    import {
        mapGetters
    } from 'vuex';
    let timer = null
    import * as cloudApi from '@/utils/cloudApi.js'
    export default {
        data() {
            return {
                avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eez8ZT1xsO67y36br9APkZQ/132',
                nickName: 'momo',
                disabled: false,
                userInfo: {},
                randUserList: [{
                    nickName: '神经蛙',
                    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eez8ZT1xsO67y36br9APkZQ/132'
                }, {
                    nickName: 'momo',
                    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280EpQFd3lbcnawk5e2rYNssdA/132'
                }, {
                    nickName: '哄哄',
                    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eiccz7HqL70daDHeR4TdAiatw/132'
                }, {
                    nickName: '阿白',
                    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q3auHgzwzM6ibB3G1OHp71sYFeGqPgNbv5TWic7Z6EicOkpDKfCbThqibZZLVLvA280Eo0O7r06qgfrFCIEAibLa63w/132'
                }]
            }
        },
        onLoad() {

            this.userInfo = uni.getStorageSync("userInfo") || getApp().globalData.userInfo
            if (this.userInfo) {
                this.nickName = this.userInfo.nickName
                this.avatarUrl = this.userInfo.avatarUrl
            }
        },
        computed: {

        },
        watch: {
            //无内容时按钮置灰
            nickName(value) {
                value ? (this.disabled = false) : (this.disabled = true);
            }
        },
        methods: {
            upImg() {
                uni.chooseImage({
                    count: 1,
                    success: async res => {
                        console.log(res);
                        if (res.tempFilePaths.length > 0) {
                            let filePath = res.tempFilePaths[0]
                            //进行上传操作

                            // promise方式
                            const result = await uniCloud.uploadFile({
                                filePath: filePath,
                                cloudPath: 'a.jpg'
                            });
                            console.log(result);
                            this.avatarUrl = result.fileID
                            // callback方式，与promise方式二选一即可
                            // uniCloud.uploadFile({
                            //     filePath: filePath,
                            //     cloudPath: 'a.jpg',
                            //     onUploadProgress: function(progressEvent) {
                            //         console.log(progressEvent);
                            //         var percentCompleted = Math.round(
                            //             (progressEvent.loaded * 100) / progressEvent.total
                            //         );
                            //     },
                            //     success() {},
                            //     fail() {},
                            //     complete() {}
                            // });

                        }
                    }
                });
            },
            getRandUser() {
                let {
                    nickName,
                    avatarUrl
                } = this.randUserList[Math.floor(Math.random() * 5)]
                if (timer !== null) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    this.nickName = nickName
                    this.avatarUrl = avatarUrl
                }, 500)

            },

            updateUserInfo() {
                uni.showLoading({
                    title: '保存中',
                    mask: true
                })
                if (timer !== null) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    cloudApi.call({
                        name: "updateUserInfo",
                        data: {
                            api: 'updateAvatar_nicknApi',
                            nickName: this.nickName,
                            avatarUrl: this.avatarUrl,
                            token: this.userInfo.token
                        },
                        success: res => {
                            console.log(res);
                        }
                    })
                    uni.hideLoading()
                    cloudApi.getUserOpenid()
                    this.$showToast('保存成功', 'none')
                }, 500)
            },
        }
    }
</script>

<style lang="scss">
    .wrap {
        .Info {
            padding: 40rpx 0 40rpx 40rpx;

            .item {
                display: flex;
                position: relative;
                align-items: center;
                padding: 15rpx 0;
                border-bottom: 0.5rpx solid #eee;
                border-top: 0.5rpx solid #eee;
                height: 150rpx;
                color: #000000;
                font-size: 30rpx;

                image {
                    width: 100rpx;
                    height: 100rpx;
                    // border: 5rpx solid #fff;
                    border-radius: 10rpx;
                }

                text {
                    margin-right: 30rpx;
                }

                .icon {
                    position: absolute;
                    right: 30rpx;
                    color: #bbb;

                }
            }

        }

        .randInfo {
            color: skyblue;
            text-align: right;
            padding: 20rpx;
            margin-bottom: 80rpx;

            .randIcon {
                padding-right: 20rpx;
            }
        }

        .btn {
            width: 300rpx;
            height: 90rpx;
            color: #ffffff;
            border-radius: 20rpx;
            text-align: center;
        }
    }
</style>
